<?php
        require 'login.php';



?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <?php
            include_once('generico.php');
            include_once('funzioni.php');
    
	generaHead();
	?>
    <body>
        <div id="container">
        <?php
            generaHeader();
            generaMenusx();
            generaMenuLogin();
	
            echo '<div id="content">';
            if(isset($_GET['stringhe'])){
				$p=0;
				$t=0;
				$stringa = cleanSimpleString($_GET['stringhe']);
			if($stringa!=""){
				$autore = cleanSimpleString($_GET['autore']);
				$dove = cleanSimpleString($_GET['dove']);				
				$type = cleanSimpleString($_GET['modo']);
				
				$ordina = cleanSimpleString($_GET['ordina']);
				$ordOfOrd = cleanSimpleString($_GET['ordine']);
				$categoria = array();
				foreach($_GET as $k=>$c){
					if (substr($k,0,9)=="categoria") {
						$categoria[]=$c;
					}
				}
				
				//$categoria = cleanSimpleString($_GET['categoria']);
				$minimo = (okDate($_GET['minimo'])) ? $_GET['minimo'] : "";
				$massimo = (okDate($_GET['massimo'])) ? $_GET['massimo'] : "";
				if($dove=="post" || $dove=="entrambi"){
					$p=1;
					$qryP = "SELECT Threads.Titolo, Threads.CreatoDa as Autore, DATE_FORMAT(Threads.CreatoIl,'%d/%m/%Y') as Data, Threads.Id From Posts, Threads WHERE Threads.Id= Posts.Thread AND "; 
					
					if($autore!="") $qryP .="Autore='$autore' AND ( ";
					else $qryP.=" (";
					
					
					foreach($categoria as $k=>$c){
						$qryP .= "Threads.Categoria='$c'";
						$qryP .= ($k!=count($categoria)-1) ? " OR " : ") AND ";
					}

					if ($minimo!=""){
						$qryP.=" Data<STR_TO_DATE('$minimo', '%d/%m/%Y') AND ";
					}
					if ($massimo!=""){
						$qryP.=" Data>STR_TO_DATE('$massimo', '%d/%m/%Y') AND ";
					}
					$stringhe = explode(' ',$stringa);	
					$qryP.=" ( ";			
					foreach($stringhe as $k=>$s){
						
						$qryP.=" Corpo REGEXP '[[:<:]]".$s."[[:>:]]' ";
						if ($k!=count($stringhe)-1) $qryP.= $type;
					}
					$qryP.=") ";
					$qryP .= " ORDER BY $ordina ";
					
					if ($ordOfOrd=="ASC" || $ordOfOrd=="DESC") $qryP.=" ".$ordOfOrd;
					
				}
				if($dove=="titolo" || $dove=="entrambi"){
					$t=1;
					$qryT = "SELECT Titolo, CreatoDa as Autore, DATE_FORMAT(CreatoIl,'%d/%m/%Y') as Data, Id FROM Threads WHERE ";
					if($autore!="") $qryT.="CreatoDa='$autore' AND (";
					else $qryT.=" ( ";
					
					foreach($categoria as $k=>$c){
						$qryT .= "Threads.Categoria='$c'";
						$qryT .= ($k!=count($categoria)-1) ? " OR " : ") AND ";
					}

					if ($minimo!=""){
						$qryT.=" CreatoIl<STR_TO_DATE('$minimo', '%d/%m/%Y') AND ";
					}
					if ($massimo!=""){
						$qryT.=" CreatoIl>STR_TO_DATE('$massimo', '%d/%m/%Y') AND ";
					}
					$stringhe = explode(' ',$stringa);	
					$qryT.=" ( ";			
					foreach($stringhe as $k=>$s){
						
						$qryT.=" Titolo REGEXP '[[:<:]]".$s."[[:>:]]' ";
						if ($k!=count($stringhe)-1) $qryT.= $type;
					}
					$qryT.=") ";
					$ordina = ($ordina!='data') ? "Autore"  : "Data";
					$qryT .= " ORDER BY $ordina ";
					if ($ordOfOrd=="ASC" || $ordOfOrd=="DESC") $qryT.=$ordOfOrd;
				}
				
				$error=0;
				if($p) $checkP = mysql_query($qryP) or $error=1;				
				if($t && !$error) $checkT = mysql_query($qryT) or $error=1;
				if (!$error && $t){
					echo '<h2 class="titolo">Titoli contenenti le parole richieste</h2>';
					
					if(mysql_num_rows($checkT)>0) {				
						generaThreads($checkT);
					}
					else echo '<p>Nessuna corrispondenza trovata</p>';
				}
				if (!$error && $p){
					
					echo '<h2 class="titolo">Post contenenti le parole richieste</h2>';
					if(mysql_num_rows($checkP)>0) {
						generaThreads($checkP);
					}else echo '<p>Nessuna corrispondenza trovata</p>';
					
				}
					
				
					
			}
			else echo '<p class="red">Non hai inserito nessun termine di ricerca!</p>';
				
		}
						
			
			echo '<h2 class="titolo">Ricerca</h2>';
						
						
						echo'
			
			<form action="cerca.php" method="GET" name="ricerca" onsubmit="return controllaCerca();">
				<fieldset>
				<legend>Cerca un contenuto</legend>
				
				<label for="stringhe">Parole da cercare</label>
				<input name="stringhe" value="'.$stringa.'">
				
				<label for="autore">Autore</label>
				<input name="autore" value="'.$autore.'">
				<label for="modo">Tipo di Ricerca</label>
				<select name="modo">
				<option value="and">Tutte le parole</option>
				<option value="or"';
				if($modo=="or") echo' selected';
				echo '>Almeno una parola</option>				
				</select>
				</fieldset>
				<fieldset>
				<legend>Dove</legend>
				
				
				
			';
				
				$qry= "SELECT Nome FROM Categorie Order By Ordine";
				$check = mysql_query($qry);
				$cats = mysql_fetch_assoc($check);
				echo '<div class="inputs">';
				while($cats){
					echo '<label>'.$cats['Nome'].'</label><input type="checkbox" value='.$cats['Nome'].' name="categoria'.$cats['Nome'].'"';
					if(!isset($categoria) || (isset($categoria) && in_array($cats['Nome'],$categoria))) echo 'checked ';
					echo '>';
					$cats = mysql_fetch_assoc($check);					
				}
				echo '</div>
				</fieldset>
				<fieldset>
				<legend>Avanzate</legend>
				
				
		<label for="dove">Cerca nel</label>
				<select name="dove">
				<option value="entrambi">Titolo del thread e nel contenuto dei post</option>
				<option value="titolo"';
				if(isset($dove) && $dove=="titolo") echo 'selected';
				echo '>Titolo del thread</option>
				<option value="post"';
				if(isset($dove) && $dove=="post") echo 'selected';
				echo '>Contenuto dei post</option>
				</select>
				<label for="minimo" >Prima del</label>
				<input id="minimo" name="minimo" value="'.$minimo.'">
				<label for="massimo" >Dopo il</label>
				<input id="massimo" name="massimo" value="'.$massimo.'">
				<label for="ordina">Ordina per</label>
				<select name="ordina" >
				<option value="data">Data</option>
				<option value="autore"';
				if($ordina="autore") echo 'selected';
				echo '>Autore</option>
				</select>
				<label for="tipoO">In ordine</label>
				<select  name="ordine">
				<option value="DESC">Decrescente</option>
				<option value="ASC"';
				if($ordOfOrd="ASC") echo 'selected';
				echo '>Crescente</option>				
				</select>
				</fieldset>
				<p id="go">
					<input type="submit" value="Cerca" name="Cerca">
					</p></form>';
			echo '</div>';
            generaFooter();
            
        ?>
        </div>
    </body>
</html>

	
